﻿function deleteDatabase(databaseLink)
{
    $.ajax({
        type: "POST",
        data: { databaseLink: selectedDatabaseLink },
        url: "/Databases/Delete",
        dataType: "json",
        success: function (result) {            
            if (result.Success)
                refreshDatabases();
            else
                showError('ERROR: ' + result.Message);
        },
        error: function (result) { showError('ERROR: ' + result.toString()); }
    });
}

function refreshDatabases()
{
    location.reload();
}

function refreshCollections(actionUrl, databaseLink, databaseID)
{
    $.ajax({
        type: "GET",
        data: "databaseLink=" + databaseLink,
        url: actionUrl,
        dataType: "html",
        success: function (html) {
            $('#pnlDatabase_' + databaseID).html(html);
            $('#pnlContent').html("");
        },
        error: function (result) { showError('ERROR: ' + result.toString()); }
    });
}

function refreshDocuments(collectionLink, collectionID)
{
    $.ajax({
        type: "GET",
        data: "collectionLink=" + collectionLink,
        url: "/Documents/DocumentsList",
        dataType: "html",
        success: function (html) {
            $('#pnlCollection_' + collectionID).html(html);            
        },
        error: function (result) { showError('ERROR: ' + result.toString()); }
    });
}

function deleteDocument(documentLink)
{
    $.ajax({
        type: "POST",
        data: { documentLink: documentLink },
        url: "/Documents/Delete",
        dataType: "json",
        success: function (result) {
            if (result.Success)
                refreshDocuments(selectedCollectionLink, selectedCollectionID);
            else
                showError('ERROR: ' + result.Message);
        },
        error: function (result) { showError('ERROR: ' + result.toString()); }
    });
}

function showError(errorMessage)
{
    $("#pnlAlert").html(errorMessage);
    $("#pnlAlert").slideDown("slow").delay(1500).slideUp();
}

function hideQueryManager()
{
    $('#pnlContent').html("");
}

function showQueryManager(selectedCollectionLink)
{
    $.ajax({
        type: "GET",
        data: "collectionLink=" + selectedCollectionLink,
        url: "/Documents/ShowQueryManager",
        dataType: "html",
        success: function (html) {            
            $('#pnlContent').html(html);
        },
        error: function (result) { showError('ERROR: ' + result.toString()); }
    });
}

function ExecuteQuery(queryText)
{
    $.ajax({
        type: "POST",
        data: { queryText: queryText, collectionLink: selectedCollectionLink },
        url: "/Documents/ExecuteQuery",
        dataType: "html",
        success: function (resultRaw) {
            var result = jQuery.parseJSON(resultRaw);
            if (result.Success)
                $('#pnlQueryResult').html(result.Values);
            else
                showError('ERROR: ' + result.Message);
        },
        error: function (result) { showError('ERROR: ' + result.toString()); }
    });
}